import Flutter
import UIKit

public class AppDynamicIconPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "app_dynamic_icon", binaryMessenger: registrar.messenger())
        let instance = AppDynamicIconPlugin()
        UIViewController.initializeMethod()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getPlatformVersion":
            result("iOS " + UIDevice.current.systemVersion)
        case "changeIcon":
            UIApplication.shared.setAlternateIconName("newAppIcon") { error in
                if error != nil {
                    print("替换图标错误=\(String(describing: error))")
                }else{
                    print("更换为新图标成功")
                }
            }
//            let alertVC = UIAlertController(title: "title", message: "测试", preferredStyle: .alert)
//            let cancelAction = UIAlertAction(title: "cancel", style: .cancel)
//            alertVC.addAction(cancelAction)
//            UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true)
        case "resetIcon":
            UIApplication.shared.setAlternateIconName(nil) { error in
                if error != nil {
                    print("替换图标错误=\(String(describing: error))")
                }else{
                    print("还原图标成功")
                }
            }
        default:
            result(FlutterMethodNotImplemented)
        }
    }
}

///去掉切换的弹框
extension UIViewController {
    
    public class func initializeMethod() {
        
        let presentSelector = #selector(UIViewController.present(_:animated:completion:))
        let swizzledPresentSelector = #selector(UIViewController.customPresent(_:animated:completion:))
        
        let presentMethod = class_getInstanceMethod(self, presentSelector)
        let swizzledPresentMethod = class_getInstanceMethod(self, swizzledPresentSelector)
        
        let didAddPresentMethod: Bool = class_addMethod(self, presentSelector, method_getImplementation(swizzledPresentMethod!), method_getTypeEncoding(presentMethod!))
        
        if didAddPresentMethod {
            class_replaceMethod(self, swizzledPresentSelector, method_getImplementation(presentMethod!), method_getTypeEncoding(presentMethod!))
        } else {
            method_exchangeImplementations(presentMethod!, swizzledPresentMethod!)
        }
    }
    
    @objc func customPresent(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if viewControllerToPresent is UIAlertController {
            if let alertController = viewControllerToPresent as? UIAlertController {
                if alertController.title == nil && alertController.message == nil {
                    print("替换图标操作")
                    return
                } else {
                    self .customPresent(viewControllerToPresent, animated: flag, completion: completion)
                    return
                }
            }
        }
        self .customPresent(viewControllerToPresent, animated: flag, completion: completion)
    }
}
